const Mock = require('mockjs')

const List = []
const count = 100

for (let i = 0; i < count; i++) {
  List.push(Mock.mock({
    id: '@increment',
    user_id: '@increment',
    user: {
      id: '@increment',
      avatar: '@image("100x100")',
      username: '@csentence(3, 6)',
      nickname: '',
      phone: '15285072859',
      user_level: '年度会员',
      user_expire: '@now',
      created_time: '@now',
      'status|1': [0, 1]
    },
    'total_consume|1': [5, 10, 15, 20],
    'no_comment|1': [0, 1],
    'no_access|1': [0, 1],
    created_time: '@now',
    updated_time: '@now'

  }))
}
const tokens = {
  admin: {
    token: 'admin-token'
  },
  editor: {
    token: 'editor-token'
  }
}

const users = {
  'admin-token': {
    roles: ['admin'],
    introduction: 'I am a super administrator',
    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
    name: 'Super Admin'
  },
  'editor-token': {
    roles: ['editor'],
    introduction: 'I am an editor',
    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
    name: 'Normal Editor'
  }
}

module.exports = [
  // 用户列表
  {
    url: '/vue-element-admin/user/list',
    type: 'get',
    response: config => {
      const {
        username,
        page = 1,
        limit = 20
      } = config.query
      const mockList = List.filter(item => {
        // if (type && item.type !== type) return false
        if (username && item.user.username.indexOf(username) < 0) return false
        return true
      })
      const pageList = mockList.filter((item, index) => index < limit * page && index >= limit * (page - 1))
      return {
        code: 20000,
        data: {
          total: mockList.length,
          items: pageList
        }
      }
    }
  },
  // 课程列表
  {
    url: '/vue-element-admin/user_course',
    type: 'get',
    response: config => {
      const {
        page = 1,
        limit = 20
      } = config.query
      const mockList = []
      for (let i = 0; i < count; i++) {
        mockList.push(Mock.mock({
          id: '@increment',
          title: '@csentence(3, 6)',
          'price|1': [10, 12, 13, 14, 15],
          created_time: '@now',
          updated_time: '@now'

        }))
      }
      const pageList = mockList.filter((item, index) => index < limit * page && index >= limit * (page - 1))
      return {
        code: 20000,
        data: {
          total: mockList.length,
          items: pageList
        }
      }
    }
  },
  // 专栏列表
  {
    url: '/vue-element-admin/user_column',
    type: 'get',
    response: config => {
      const {
        page = 1,
        limit = 20
      } = config.query
      const mockList = []
      for (let i = 0; i < count; i++) {
        mockList.push(Mock.mock({
          id: '@increment',
          title: '@csentence(3, 6)',
          'price|1': [10, 12, 13, 14, 15],
          created_time: '@now',
          updated_time: '@now'

        }))
      }
      const pageList = mockList.filter((item, index) => index < limit * page && index >= limit * (page - 1))
      return {
        code: 20000,
        data: {
          total: mockList.length,
          items: pageList
        }
      }
    }
  },
  // 专栏列表
  {
    url: '/vue-element-admin/user_order',
    type: 'get',
    response: config => {
      const {
        page = 1,
        limit = 20
      } = config.query
      const mockList = []
      for (let i = 0; i < count; i++) {
        mockList.push(Mock.mock({
          id: '@increment',
          no: '@increment',
          title: '@csentence(3, 6)',
          'status|1': ['pendding', 'success', 'fail'],
          'price|1': [10, 12, 13, 14, 15],
          created_time: '@now',
          updated_time: '@now'

        }))
      }
      const pageList = mockList.filter((item, index) => index < limit * page && index >= limit * (page - 1))
      return {
        code: 20000,
        data: {
          total: mockList.length,
          items: pageList
        }
      }
    }
  },
  // 观看历史
  {
    url: '/vue-element-admin/user_history',
    type: 'get',
    response: config => {
      const {
        page = 1,
        limit = 20
      } = config.query
      const mockList = []
      for (let i = 0; i < count; i++) {
        mockList.push(Mock.mock({
          id: '@increment',
          title: '@csentence(3, 6)',
          'type|1': ['audio', 'column', 'video', 'media'],
          'time|1': [10, 12, 13, 14, 15],
          created_time: '@now',
          updated_time: '@now'

        }))
      }
      const pageList = mockList.filter((item, index) => index < limit * page && index >= limit * (page - 1))
      return {
        code: 20000,
        data: {
          total: mockList.length,
          items: pageList
        }
      }
    }
  },
  // 观看历史
  {
    url: '/vue-element-admin/user_comment',
    type: 'get',
    response: config => {
      const {
        page = 1,
        limit = 20
      } = config.query
      const mockList = []
      for (let i = 0; i < count; i++) {
        mockList.push(Mock.mock({
          id: '@increment',
          content: '@csentence(10, 15)',
          title: '@csentence(3, 6)',
          'type|1': ['audio', 'column', 'video', 'media'],
          'time|1': [10, 12, 13, 14, 15],
          created_time: '@now',
          updated_time: '@now'
        }))
      }
      const pageList = mockList.filter((item, index) => index < limit * page && index >= limit * (page - 1))
      return {
        code: 20000,
        data: {
          total: mockList.length,
          items: pageList
        }
      }
    }
  },
  // 查询用户详情
  {
    url: '/vue-element-admin/user_detail',
    type: 'get',
    response: config => {
      const {
        id
      } = config.query
      const data = List.filter((item) => item.user.id === parseInt(id))[0]
      return {
        code: 20000,
        data
      }
    }
  },
  // 修改用户无法评论
  {
    url: '/vue-element-admin/user_nocomment',
    type: 'get',
    response: config => {
      // const {
      //   id,status
      // } = config.query
      return {
        code: 20000,
        data: true
      }
    }
  },
  // 修改用户无法查看
  {
    url: '/vue-element-admin/user_noaccess',
    type: 'get',
    response: config => {
      // const {
      //   id,status
      // } = config.query
      return {
        code: 20000,
        data: true
      }
    }
  },
  // user login
  {
    url: '/vue-element-admin/user/login',
    type: 'post',
    response: config => {
      const {
        username
      } = config.body
      const token = tokens[username]

      // mock error
      if (!token) {
        return {
          code: 60204,
          message: 'Account and password are incorrect.'
        }
      }

      return {
        code: 20000,
        data: token
      }
    }
  },

  // get user info
  {
    url: '/vue-element-admin/user/info\.*',
    type: 'get',
    response: config => {
      const {
        token
      } = config.query
      const info = users[token]

      // mock error
      if (!info) {
        return {
          code: 50008,
          message: 'Login failed, unable to get user details.'
        }
      }

      return {
        code: 20000,
        data: info
      }
    }
  },

  // user logout
  {
    url: '/vue-element-admin/user/logout',
    type: 'post',
    response: _ => {
      return {
        code: 20000,
        data: 'success'
      }
    }
  }
]
